package com.example.lastwork;

import static java.lang.System.out;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class other_person_page extends AppCompatActivity {
    private String TAG = "other_person_page";
    private String id;
    List<Map<String,String>> data_list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_other_person_page);

        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        Log.i(TAG,"id="+id);

        TextView id1 = findViewById(R.id.a_o_id);
        TextView name1 = findViewById(R.id.a_o_user);
        GridView gridView = findViewById(R.id.o_p_c);
        new Thread(){
            @Override
            public void run() {
                try {
                    UserDao userDao = new UserDao();
                    userDao.getpersonData(data_list,id);
                    String name = UserDao.getuser(id);
                    Log.i(TAG, name);
                    Log.i(TAG, id);
                    id1.setText(id);
                    name1.setText(name);
                    Log.i(TAG, name);
                    Log.i(TAG,"成功");
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.i(TAG, "失败");
                }
                if(data_list==null){
                    out.println("该用户尚未发布文章");
                }
                String [] from ={"name","title","id"};
                int [] to = {R.id.T_id,R.id.title,R.id.T_myid};

                other_person_page.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        SimpleAdapter adapter = new SimpleAdapter(other_person_page.this, data_list, R.layout.gridviewcontent, from, to);
                        gridView.setAdapter(adapter);
                    }
                });

                Log.i(TAG, "线程运行");
            }

        }.start();
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Intent intent = new Intent(other_person_page.this,
                        details.class);
                String num = data_list.get(position).get("num");
                Log.i(TAG,num);
                intent.putExtra("num",num);
                String id1 =data_list.get(position).get("id");
                intent.putExtra("id",id1);
                startActivity(intent);
            }
        });
    }
}